package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Inventory;
import org.csu.petserver.domain.Item;
import org.csu.petserver.domain.Product;
import org.csu.petserver.persistence.InventoryMapper;
import org.csu.petserver.persistence.ItemMapper;
import org.csu.petserver.service.ItemInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
@Repository("ItemInfo")
@Service
@MapperScan("org.csu.petserver.persistence")
public class ItemInfoImpl implements ItemInfo {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private Inventory inventory;
    @Override
    public List<Item> searchItemInCertainProduct(Product product, String keyword) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getId())
                .like("itemid",keyword);
        List<Item> items = itemMapper.selectList(queryWrapper);
        return items;
    }

    @Override
    public boolean addItem(Item item, Integer quantity) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",item.getId());
        if(itemMapper.selectOne(queryWrapper) != null){
            return false;
        }
        else {
            itemMapper.insert(item);
            inventory.setId(item.getId());
            inventory.setQuantity(quantity);
            inventoryMapper.insert(inventory);
            return true;
        }
    }

    @Override
    public boolean updateItem(Item item) {
        itemMapper.updateById(item);
        return true;
    }

    @Override
    public boolean deleteItem(Item item) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",item.getId());
        if(itemMapper.selectOne(queryWrapper) == null){
            return false;
        }
        else {
            itemMapper.deleteById(item);
            QueryWrapper<Inventory> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("itemid",item.getId());
            Inventory inventory = inventoryMapper.selectOne(queryWrapper1);
            if(inventory != null){
                inventoryMapper.deleteById(inventory);
            }
            return true;
        }
    }

    @Override
    public List<Item> sortByListPriceInCertainProduct(Product product) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getId());
        List<Item> items = itemMapper.selectList(queryWrapper);
        items.sort((t1,t2)->t2.getListprice().compareTo(t1.getListprice()));
        return items;
    }

    @Override
    public List<Item> sortByUnitCostInCertainProduct(Product product) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getId());
        List<Item> items = itemMapper.selectList(queryWrapper);
        items.sort((t1,t2)->t2.getUnitcost().compareTo(t1.getUnitcost()));
        return items;
    }

    @Override
    public Item getItem(String itemId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);
        Item item=itemMapper.selectOne(queryWrapper);
        return  item;
    }
}
